#include <stdio.h>
#include <locale.h>
#include <libsoup/soup.h>
#define github
//baf bfv
void form (char *method, char *url, char *val_value)
{
//  char *res = NULL;
  //res = setlocale(LC_ALL, "zh_CN.UTF-8");
  //res = setlocale(LC_ALL, "C.UTF-8");
  setlocale(LC_ALL, "C.UTF-8");
//  g_print ("%s 中文\n", res);
  SoupSession* session = soup_session_new();
  //SoupMessage* msg = soup_message_new("POST", "https://example.com");
  SoupMessage* msg = NULL;
  SoupMultipart* multipart = soup_multipart_new(SOUP_FORM_MIME_TYPE_MULTIPART);
#ifdef github
  soup_multipart_append_form_string(multipart, "val", val_value);
  soup_multipart_append_form_string(multipart, "authenticity_token",
      "7sH+Syp1Yy5gfd2Jd0PnFWp9HCgF9zbLBKRIAuoBjqaEaowDDrK/hzpP6wJsp/DX4qy443f7TqZyyNLFF2fLKA==");
  msg = soup_form_request_new_from_multipart (url, multipart);
  soup_message_headers_append(msg->request_headers, "Cookie",
  "_octo=GH1.1.1122610956.1690256732; logged_in=no; preferred_color_mode=light; tz=Asia%2FShanghai; _device_id=ccccecb8db702f56a49e3073d7b20f6f; _gh_sess=5r%2BFMHK9acNhvtakNJNZSBNa79M8Uwp%2BWoclcOF0r7SmoLiy%2BanmjSwY6TOupn34qLCjI9mUbi0rG%2FAfjruX%2FmNafAmkTflosiqHCSBg7pc9%2Bix4Izqxwd5PoepYRQxjPkbvPKhNalLeR%2FhLF43%2BxVgrg4AJjfAE7cO36hxQioLOFwtz7wuYdYWtMXX8mDDrRHQhXgKG9Zkvn7I7Sax1lqW7PWmnS3baPFzH%2BLsu0vKmNUBNexDmPtMTFmqunnp8g7STO23%2FLIOPHiw3gXMTRgyFEdko6v7jbQ%3D%3D--pSnPcT3sFoExLDvM--Sxu9a%2F5Wu2M0ngU5LVfbbQ%3D%3D");
#endif
#ifdef gitee
  soup_multipart_append_form_string(multipart, "val", val_value);
  soup_multipart_append_form_string(multipart, "do", "user_username");
  msg = soup_form_request_new_from_multipart (url, multipart);
#endif
#ifdef m163
  soup_multipart_append_form_string(multipart, "cmd", "urs.checkNameAndReco");
  soup_multipart_append_form_string(multipart, "domain", "163.com");
  gchar buf[1024] = {0}, sid[65] = {0};
  g_snprintf (buf, sizeof (buf), "%"G_GUINT64_FORMAT, g_get_real_time()/1000);
  g_print ("time:%s\n", buf);
  //soup_multipart_append_form_string(multipart, "jsessionid", "1690262358102");
                                                              //1690263770156
  soup_multipart_append_form_string(multipart, "jsessionid", buf);
  soup_multipart_append_form_string(multipart, "name", val_value);
  soup_multipart_append_form_string(multipart, "sdid", "1001093008429281280");
  generate_session_id (sid);
  g_print ("sid:%s\n", sid);
  soup_message_headers_append(msg->request_headers, "Cookie", "JSESSIONID=37DA679A56870CBD596287895A4AAD9A; starttime=");
  msg = soup_form_request_new_from_multipart (url, multipart);
#endif
  soup_session_send_message(session, msg);
  SoupMessageHeadersIter iter;
  const char *hname, *value;
  soup_message_headers_iter_init(&iter, msg->response_headers);
  while(soup_message_headers_iter_next(&iter, &hname, &value)){
    //g_message("%s: %s", hname, value);
  }
  g_print("status code: %d\t", msg->status_code);
//  g_print("body: %s\n", msg->response_body->data);
  if (g_strcmp0 ("地址已存在", msg->response_body->data)) {
    g_print("body: %s\n", msg->response_body->data);
    g_print("val_value: %s\n", val_value);
  }
 // printf("response body: %s\n", msg->response_body->data);

  g_object_unref(msg);
  g_object_unref(session);
  return;
}
int main(int argc, char **argv)
{
  gchar *method = "POST", value[1024] = {0};
#ifdef gitee
  gchar *uri = "https://gitee.com/check", c1, c2, c3;
  for (c1 = 'a'; c1 <= 'z'; c1++) {
    for (c2 = 'a'; c2 <= 'z'; c2++) {
      for (c3 = 'a'; c3 <= 'z'; c3++) {
        //g_snprintf (value, sizeof (value), "%c%c", c1, c2);
        g_snprintf (value, sizeof (value), "%c%c%c", c1, c2, c3);
   //     g_print ("p:%s \n", value);
        form (method, uri, value);
      }
    }
  }
#endif
#ifdef m163
  gchar *uri = "https://regapi.mail.163.com/unireg/call.do";
  gint i = 0;
  for (i = 0; i < 1000; i++) {
    g_snprintf (value, sizeof (value), "irc%03d", i);
    g_print("name: %s\n", value);
    form (method, uri, value);
  }
#endif
#ifdef github
  gchar *uri = "https://github.com/signup_check/username";
  gint i = 0;
  for (i = 0; i < 10000; i++) {
    g_snprintf (value, sizeof (value), "%04d", i);
    g_print("name: %s\n", value);
    form (method, uri, value);
  }
#endif
  return 1;
}
